import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shensu/common/model/game.dart';
import 'package:flutter_shensu/common/util/network_utils.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AndroidGamePageState extends State<StatefulWidget>
    with AutomaticKeepAliveClientMixin {
  List<Game> _gameList = [];

  @override
  void initState() {
    super.initState();
    getAllGameList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(backgroundColor: Colors.white, body: _getBody());
  }

  void getAllGameList() async {
    if (_gameList.length == 0) {
      if (mounted) {
        List<Game> future = await NetWorkUtils.instance.getAllGameList();
        setState(() {
          _gameList = future;
        });
      }
    }
  }

  Widget _getBody() {
    if (_gameList.length != 0) {
      MediaQueryData mediaQueryData = MediaQuery.of(context);

      return new GridView.count(
        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
        crossAxisCount: 4,
        primary: false,
        childAspectRatio: mediaQueryData.size.height / 1000,
        children: _gameList.map((Game g) {
          return _getGameItemWidget(context, g);
        }).toList(),
      );
    } else {
      return Center(
        child: CupertinoActivityIndicator(),
      );
    }
  }

  Widget _getGameItemWidget(BuildContext context, Game g) {
    return new Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          width: 54.0,
          height: 54.0,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(
            Radius.circular(15.0),
          )),
          child: CachedNetworkImage(
            imageUrl: g.game_icon_url,
            placeholder: new CircularProgressIndicator(
              strokeWidth: 2.0,
            ),
            errorWidget: new Icon(Icons.error),
          ),
        ),
        Padding(
          padding: new EdgeInsets.fromLTRB(0, 8, 0, 0),
        ),
        Text(
          g.game_name_zh,
          textAlign: TextAlign.center,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: 12.0),
        ),
        ButtonTheme(
          minWidth: 30,
          height: 20,
          child: OutlineButton(
            highlightColor: Colors.transparent,
            // 按钮按下的背景颜色
            splashColor: Colors.transparent,
            // 点击时,水波动画的颜色
            highlightElevation: 0.0,
            //按下时的阴影
            onPressed: () {
              _showToast('当前游戏暂未开放');
            },
            child: Icon(
              Icons.arrow_downward,
              color: Color(0xFF00DCC8),
              size: 10,
            ),
            borderSide: new BorderSide(color: Color(0xFF00DCC8)),
          ),
        )
      ],
    );
  }

  void _showToast(var message) {
    Fluttertoast.showToast(
        msg: message, toastLength: Toast.LENGTH_SHORT, timeInSecForIos: 1);
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
